/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.approvalorder.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.approvalorder.entity.BizApprovalOrder;
import vip.xiaonuo.biz.modular.approvalorder.param.*;
import vip.xiaonuo.biz.modular.approvalorder.result.BizApprovalCanteenResult;
import vip.xiaonuo.biz.modular.approvalorder.result.BizApprovalDeptResult;
import vip.xiaonuo.biz.modular.approvalorder.service.AbcPayService;
import vip.xiaonuo.biz.modular.approvalorder.service.BizApprovalOrderService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import java.util.List;

/**
 * 签到支付记录控制器
 *
 * @author fqa
 * @date 2025/05/16 10:36
 */
@Tag(name = "签到支付记录控制器")
@RestController
@Validated
public class BizApprovalOrderController {

    @Resource
    private BizApprovalOrderService bizApprovalOrderService;

    /**
     * 获取签到支付记录分页
     *
     * @author fqa
     * @date 2025/05/16 10:36
     */
    @Operation(summary = "获取签到支付记录分页")
    @SaCheckPermission("/biz/approvalorder/page")
    @GetMapping("/biz/approvalorder/page")
    public CommonResult<Page<BizApprovalOrder>> page(BizApprovalOrderPageParam bizApprovalOrderPageParam) {
        return CommonResult.data(bizApprovalOrderService.page(bizApprovalOrderPageParam));
    }


    /**
     * 用户支付记录
     *
     * @author fqa
     * @date 2025/05/16 10:36
     */
    @Operation(summary = "获取签到支付记录分页")
    @GetMapping("/biz/approvalorder/userApprovalOrderPage")
    public CommonResult<Page<BizApprovalOrder>> userApprovalOrderPage(BizUserApprovalOrderPageParam bizApprovalOrderPageParam) {
        return CommonResult.data(bizApprovalOrderService.userApprovalOrderPage(bizApprovalOrderPageParam));
    }

    /**
     * 食堂支付记录
     *
     * @author fqa
     * @date 2025/05/16 10:36
     */
    @Operation(summary = "获取签到支付记录分页")
    @GetMapping("/biz/approvalorder/canteenApprovalOrderPage")
    public CommonResult<Page<BizApprovalOrder>> canteenApprovalOrderPage(BizUserApprovalOrderPageParam bizApprovalOrderPageParam) {
        return CommonResult.data(bizApprovalOrderService.canteenApprovalOrderPage(bizApprovalOrderPageParam));
    }

    /**
     * 获取签到支付记录详情
     *
     * @author fqa
     * @date 2025/05/16 10:36
     */
    @Operation(summary = "获取签到支付记录详情")
    @GetMapping("/biz/approvalorder/detail")
    public CommonResult<BizApprovalOrder> detail(@Valid BizApprovalOrderIdParam bizApprovalOrderIdParam) {
        return CommonResult.data(bizApprovalOrderService.detail(bizApprovalOrderIdParam));
    }

    @Operation(summary = "创建订单")
    @PostMapping("/biz/approvalorder/createOrder")
    public CommonResult<String> createOrder(@RequestBody @Valid BizApprovalCreateOrderParam bizApprovalCreateOrderParam) {
        BizApprovalOrder order = bizApprovalOrderService.createOrder(bizApprovalCreateOrderParam);
        return CommonResult.data(order.getId());
    }

    /**
     * 部门支付信息
     *
     * @param bizApprovalDeptPageParam
     * @return
     */
    @Operation(summary = "部门支付信息")
    @SaCheckPermission("/biz/approvalorder/deptList")
    @GetMapping("/biz/approvalorder/deptList")
    public CommonResult<List<BizApprovalDeptResult>> deptList(BizApprovalDeptPageParam bizApprovalDeptPageParam) {
        return CommonResult.data(bizApprovalOrderService.deptList(bizApprovalDeptPageParam));
    }


    /**
     * 食堂支付信息
     * @return
     */
    @Operation(summary = "部门支付信息")
    @SaCheckPermission("/biz/approvalorder/canteenOrderList")
    @GetMapping("/biz/approvalorder/canteenOrderList")
    public CommonResult<List<BizApprovalCanteenResult>> canteenOrderList(BizApprovalCanteenOrderPageParam bizApprovalCanteenOrderPageParam) {
        return CommonResult.data(bizApprovalOrderService.canteenOrderList(bizApprovalCanteenOrderPageParam));
    }

    /**
     * 部门订单信息
     *
     * @param bizApprovalDeptPageParam
     * @return
     */
    @Operation(summary = "部门支付信息")
    @SaCheckPermission("/biz/approvalorder/exportDeptList")
    @GetMapping("/biz/approvalorder/exportDeptList")
    public CommonResult<ResponseEntity<byte[]>> exportDeptList(BizApprovalDeptPageParam bizApprovalDeptPageParam) {
        return CommonResult.data(bizApprovalOrderService.exportDeptList(bizApprovalDeptPageParam));
    }

    /**
     * 食堂支付信息
     *
     * @param bizApprovalCanteenOrderPageParam
     * @return
     */
    @Operation(summary = "食堂支付信息")
    @SaCheckPermission("/biz/approvalorder/exportCanteenOrderList")
    @GetMapping("/biz/approvalorder/exportCanteenOrderList")
    public CommonResult<ResponseEntity<byte[]>> exportCanteenOrderList(BizApprovalCanteenOrderPageParam bizApprovalCanteenOrderPageParam) {
        return CommonResult.data(bizApprovalOrderService.exportCanteenOrderList(bizApprovalCanteenOrderPageParam));
    }

    @Resource
    private AbcPayService abcPayService;

    /**
     * 微信支付
     *
     * @param orderId 订单ID
     */
    @PostMapping("/biz/approvalorder/pay")
    public CommonResult<JSONObject> pay(String orderId) {
        return CommonResult.data(abcPayService.createWxOrder(orderId));
    }


    /**
     * 微信支付回调用
     *
     */
    @RequestMapping("/biz/approvalorder/resultNotify")
    public void resultNotify(@RequestBody String paymentResult) {
        abcPayService.resultNotify(paymentResult);
    }



    /**
     * 删除签到支付记录
     *
     * @author fqa
     * @date  2025/05/29 12:28
     */
    @Operation(summary = "删除签到支付记录")
    @CommonLog("删除签到支付记录")
    @SaCheckPermission("/biz/approvalorder/delete")
    @PostMapping("/biz/approvalorder/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                       List<BizApprovalOrderIdParam> bizApprovalOrderIdParamList) {
        bizApprovalOrderService.delete(bizApprovalOrderIdParamList);
        return CommonResult.ok();
    }
}
